package com.coffee.framework.core.satoken;

import cn.dev33.satoken.interceptor.SaAnnotationInterceptor;
import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * Sa Token 注册拦截器
 *
 * @author Kevin
 */
@Slf4j
@Configuration
public class SaTokenConfig implements WebMvcConfigurer {

    private static final List<String> IGNORE_URL = Lists.newArrayList();

    static {
        IGNORE_URL.add("/");
        IGNORE_URL.add("/index");
        IGNORE_URL.add("/login");
        IGNORE_URL.add("/logout");
        IGNORE_URL.add("/common/**");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册路由拦截器，自定义认证规则
        registry.addInterceptor(new SaRouteInterceptor((req, res, handler) -> {
            SaRouter.match("/**")
                    .notMatch(IGNORE_URL)
                    .check(() -> {
                        StpUtil.checkLogin();
                    });
        }));
        registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
    }

}
